# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pip
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=19.3.1
pkgrel=3
pkgdesc="An easy_install replacement for installing pypi python packages (mingw-w64)"
arch=('any')
license=('MIT')
url="https://pip.pypa.io/"
# "distro" library doesn't support windows
_deps=('setuptools' 'appdirs' 'cachecontrol' 'colorama' 'distlib' #'distro' 
       'html5lib' 'lockfile' 'msgpack' 'packaging' 'pep517' 'progress' 'pyparsing' 'pytoml'
       'requests' 'retrying' 'six' 'webencodings' 'contextlib2')
makedepends=("${_deps[@]/#/${MINGW_PACKAGE_PREFIX}-python3-}" "${_deps[@]/#/${MINGW_PACKAGE_PREFIX}-python2-}" 
             "${MINGW_PACKAGE_PREFIX}-python2-ipaddress"
             "${MINGW_PACKAGE_PREFIX}-python3-sphinx")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/pypa/pip/archive/${pkgver}.tar.gz)
noextract=(${_realname}-${pkgver}.tar.gz)
sha256sums=('f12b7a6be2dbbfeefae5f14992c89175ef72ce0fe96452b4f66be855a12841ff')

shopt -s extglob

prepare() {
# work around symlinks in pip package that can cause problems with some building
# where dirs are not removed after being used.
  rm -rf ${_realname}-${pkgver}/tests/data/packages/symlinks/docs || true 2> /dev/nul
  bsdtar zxf ${_realname}-${pkgver}.tar.gz || true

# specify that third-party stuff should not be bundled.  This is from Archlinux
  rm -rf ${_realname}-${pkgver}/src/pip/_vendor/!(__init__.py)
  sed -i -e 's/DEBUNDLED = False/DEBUNDLED = True/' \
         -e '/cachecontrol/a\    vendored("pep517")' \
        ${_realname}-${pkgver}/src/pip/_vendor/__init__.py
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH}| true
    cp -r "${_realname}-${pkgver}" "python${pver}-build-${CARCH}"
  done
}

build() {
  for pver in {2,3}; do
    msg "Python ${pver} build for ${CARCH}" 
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

package_python2-pip() {
  depends=("${_deps[@]/#/${MINGW_PACKAGE_PREFIX}-python2-}"
           "${MINGW_PACKAGE_PREFIX}-python2-ipaddress")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}<19.1.1-2")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  # Remove conflicted files
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/pip{.exe,.exe.manifest,-script.py}

  install -D -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

}

package_python3-pip() {
  depends=("${_deps[@]/#/${MINGW_PACKAGE_PREFIX}-python3-}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}<19.1.1-2")

  local _mingw_prefix=$(cygpath -wm ${MINGW_PREFIX})

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build -v

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

  install -D -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_mingw-w64-i686-python2-pip() {
  install=${_realname}2-${CARCH}.install
  package_python2-pip
}

package_mingw-w64-i686-python3-pip() {
  install=${_realname}3-${CARCH}.install
  package_python3-pip
}

package_mingw-w64-x86_64-python2-pip() {
  install=${_realname}2-${CARCH}.install
  package_python2-pip
}

package_mingw-w64-x86_64-python3-pip() {
  install=${_realname}3-${CARCH}.install
  package_python3-pip
}
